package cn.niit.controller;

import cn.niit.entity.Classes;
import cn.niit.entity.Department;
import cn.niit.entity.Major;
import cn.niit.entity.Teacher;
import cn.niit.entity.query.ClassQuery;
import cn.niit.entity.query.TeacherQuery;
import cn.niit.service.ClassService;
import cn.niit.service.DepartmentService;
import cn.niit.service.MajorService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/class")
public class ClassController {
    @Resource
    private ClassService classService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private MajorService majorService;
    @GetMapping("/query")
    public String queryByClass(Model model, ClassQuery classQuery) {
        List<Department> departmentList =departmentService.queryAllDepartmentList();
        model.addAttribute("departmentList",departmentList);
        List<Major> majorList=majorService.queryAllMajorList();

        model.addAttribute("majorList",majorList);
        Long departmentCode=0L;
        departmentCode=departmentList.get(0).getDepartmentCode();
        PageInfo<Classes> classesPageInfo = classService.queryByClass(classQuery);
        model.addAttribute("page", classesPageInfo);
        return "/class/class";
    }
    @GetMapping("/select")
    public String select(Model model,ClassQuery classQuery){
        List<Department> departmentList =departmentService.queryAllDepartmentList();
        model.addAttribute("departmentList",departmentList);
        List<Major> majorList=majorService.queryAllMajorList();

        model.addAttribute("majorList",majorList);
        PageInfo<Classes> classesPageInfo = classService.select(classQuery);
        model.addAttribute("page", classesPageInfo);
        return "/class/class";
    }

    @GetMapping("/edit/{classCode}")
    public String toEdit(@PathVariable Integer classCode, Model model) {
        model.addAttribute("class", classService.queryClassById(classCode));
        List<Major> majorList=majorService.queryAllMajorList();
        model.addAttribute("majorList",majorList);
        List<Department> departmentList =departmentService.queryAllDepartmentList();
        model.addAttribute("departmentList",departmentList);
        return "/class/editClass";
    }

    @PostMapping("/edit")
    public String edit(Classes classes) {
        ClassQuery classQuery = new ClassQuery();
        Integer classCode = classes.getClassCode();
        classQuery.setDepartmentCode(classes.getDepartmentCode());
        PageInfo<Classes> classesPageInfo = classService.queryByDepartmentCode(classQuery);
        boolean b;
        if (classCode != null) {
            b = classService.editClass(classes);
        } else {
            b = classService.addClass(classes);
        }
        return "redirect:/class/query";
    }

    @GetMapping("/update")
    public String update(Model model) {
        List<Major> majorList=majorService.queryAllMajorList();
        model.addAttribute("majorList",majorList);
        List<Department> departmentList =departmentService.queryAllDepartmentList();
        model.addAttribute("departmentList",departmentList);
        Classes classes = new Classes();
        model.addAttribute("class", classes);
        return "/class/editClass";
    }

    @GetMapping("/delete/{classCode}")
    public String deleteClassById(@PathVariable("classCode") Integer classCode) {
        boolean b = classService.deleteClassById(classCode);
        return "redirect:/class/query";
    }
}